﻿Class Application

    ' Application-level events, such as Startup, Exit, and DispatcherUnhandledException
    ' can be handled in this file.
    Shared mtx As System.Threading.Mutex

    ''' <summary>
    ''' InitializeComponent
    ''' </summary>
    Public Sub InitializeComponent()
        ' /p:####### (window handle)
        If Environment.GetCommandLineArgs().Length > 1 AndAlso Environment.GetCommandLineArgs()(1).ToLower().StartsWith("/c") Then
            Dim created As Boolean
            mtx = New System.Threading.Mutex(True, "C4F-WhileAway", created)

            ' If we created the mutex then admin mode isn't already runing.  Otherwise
            ' no StartupUri and we will die.
            If created Then
                Me.StartupUri = New System.Uri("AdminWindow.xaml", System.UriKind.Relative)
            End If
        ElseIf Environment.GetCommandLineArgs().Length < 2 OrElse Environment.GetCommandLineArgs()(1).ToLower().StartsWith("/s") Then
            Me.StartupUri = New System.Uri("ScreenSaverWindow.xaml", System.UriKind.Relative)
        End If

        ' No support for preview (/p)
    End Sub

    ''' <summary>
    ''' Application Entry Point.
    ''' </summary>
    <System.STAThreadAttribute()> _
    Public Shared Sub Main()
        Dim app As New Application()
        app.InitializeComponent()

        If app.StartupUri IsNot Nothing Then
            app.Run()
        Else
            app.Shutdown(0)
        End If

        If Not mtx Is Nothing Then
            mtx.Close()
        End If
    End Sub

    Public Shared Sub LaunchSelf(ByVal mode As LaunchMode)
        Try
            Dim arg As String = (If(mode = LaunchMode.Config, "/c", "/s"))
            System.Diagnostics.Process.Start(System.Reflection.Assembly.GetEntryAssembly().Location, arg)
        Catch
            ' TODO: Should log failure to launch config mode
        End Try
    End Sub
End Class

Public Enum LaunchMode
    Config = 0
    Screensaver = 1
End Enum



